<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="mysql,">










<meta name="description" content="MySQL的数据文件是存储在硬盘上的，如果每次都直接从硬盘读取数据，那么会占用很多的I/O时间，所以在innoDB中加入了缓存，把数据读到内存后，并没有直接释放，而是缓存起来。">
<meta name="keywords" content="mysql">
<meta property="og:type" content="article">
<meta property="og:title" content="Bufferpool">
<meta property="og:url" content="https://arrycoder.gitee.io/mysql/bufferPool/index.html">
<meta property="og:site_name" content="arry&#39;s blog">
<meta property="og:description" content="MySQL的数据文件是存储在硬盘上的，如果每次都直接从硬盘读取数据，那么会占用很多的I/O时间，所以在innoDB中加入了缓存，把数据读到内存后，并没有直接释放，而是缓存起来。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://arrycoder.gitee.io/blog/mysql/bufferPool/bufferpool.png">
<meta property="og:image" content="https://arrycoder.gitee.io/blog/mysql/bufferPool/free.png">
<meta property="og:image" content="https://arrycoder.gitee.io/blog/mysql/bufferPool/flush">
<meta property="og:image" content="https://arrycoder.gitee.io/blog/mysql/bufferPool/lru">
<meta property="og:image" content="https://arrycoder.gitee.io/blog/mysql/bufferPool/b.png">
<meta property="og:updated_time" content="2020-06-24T06:51:56.892Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Bufferpool">
<meta name="twitter:description" content="MySQL的数据文件是存储在硬盘上的，如果每次都直接从硬盘读取数据，那么会占用很多的I/O时间，所以在innoDB中加入了缓存，把数据读到内存后，并没有直接释放，而是缓存起来。">
<meta name="twitter:image" content="https://arrycoder.gitee.io/blog/mysql/bufferPool/bufferpool.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://arrycoder.gitee.io/mysql/bufferPool/">





  <title>Bufferpool | arry's blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?97f9adfda8d04eb91dd054a98264d191";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">arry's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            Tags
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/mysql/bufferPool/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Bufferpool</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2019-10-28T16:24:25+08:00">
                2019-10-28
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/mysql/" itemprop="url" rel="index">
                    <span itemprop="name">mysql</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>MySQL的数据文件是存储在硬盘上的，如果每次都直接从硬盘读取数据，那么会占用很多的I/O时间，所以在innoDB中加入了缓存，把数据读到内存后，并没有直接释放，而是缓存起来。</p>
<a id="more"></a>

<h2 id="BufferPool"><a href="#BufferPool" class="headerlink" title="BufferPool"></a>BufferPool</h2><p>BufferPool就是InnodB中的缓存池。MySQL在启动的时候，会根据配置参数（innodb_buffer_pool_size）向内存申请一块连续的内存空间来作为缓存。在BufferPool中缓存的单位也是页（16kb），同时，每一个缓存页都有一个控制块，控制块包含了该页的一些信息，如表空间编号，页号，缓存页页号等等。同时在bufferPool中有许多链表来对缓存页进行管理。</p>
<p>比如free链表（未分配的页空间）、flush链表（脏页待刷盘的）、lru链表（对缓存的权重进行管理，没用的缓存页会被覆盖）</p>
<h2 id="结构"><a href="#结构" class="headerlink" title="结构"></a>结构</h2><p><img src="/blog/mysql/bufferPool/bufferpool.png" alt="bufferpool"></p>
<p>数据库启动时候向内存申请的空间如图所示，每一个控制块对应着一个缓存页，碎片空间则是这块连续空间中已经不够一页大小的空间碎片了，但如果分配数值刚好，这个碎片也有可能不存在。</p>
<h2 id="free链表-空闲空间"><a href="#free链表-空闲空间" class="headerlink" title="free链表 空闲空间"></a>free链表 空闲空间</h2><p>当系统刚启动时，所有缓存页都是空闲的。而在系统不断运行过程之中，这些缓存页就会被占用。innoDB会维护一个free链表。free链表的基节点包含着free链表的start和end，而链表中的元素就是控制块，控制块里有一个上个free节点，和下一个free节点的信息。</p>
<p><img src="/blog/mysql/bufferPool/free.png" alt="free"></p>
<p>所以每次需要从磁盘加载一页数据到缓存，就可以直接从这个free链表取一个空的缓存页，把缓存页的信息写到控制块里，然后移除该控制块。</p>
<h2 id="flush链表"><a href="#flush链表" class="headerlink" title="flush链表"></a>flush链表</h2><p>当bufferPool缓存页中的数据更新的时候，就会导致和磁盘上的数据不一致了，这时候，这个缓存页就被称为dirty page。这时候为了一致，就会进行同步。这时候innoDB就设计了一个flush链表来维护这些脏页，同样有一个基节点。</p>
<p>维护着这些信息，以便于在后面某个时间点将数据刷新到磁盘文件上。</p>
<p><img src="/blog/mysql/bufferPool/flush" alt="flush"></p>
<h2 id="缓存管理"><a href="#缓存管理" class="headerlink" title="缓存管理"></a>缓存管理</h2><p>BufferPool内存空间是有大小的，总会有用完的时候，所以在缓存页大小已经超过了bufferpool了，那么就需要把一部分旧的缓存从bufferPool中移除。</p>
<h3 id="LRU链表"><a href="#LRU链表" class="headerlink" title="LRU链表"></a>LRU链表</h3><p>LRU（Least recently used）是指把最近最少使用的数据移除掉。因此在查询数据的时候，如果</p>
<ol>
<li>缓存中没有这个数据，那么把数据读取到缓存中，并把这个数据页放到头节点。</li>
<li>如果缓存命中了，那么把该页直接移动到头节点。3</li>
</ol>
<p>简单的LRU链表会因为一些情况导致问题。</p>
<ul>
<li><p>预读：</p>
<ul>
<li>线性预读：如果顺序读取了某个区的连续数量的页，则会异步读取下一个区中的所有页到bufferpool中。</li>
<li>随机预读：已经缓存了某个区内的连续数量的页，则会异步读取这个区所有的数据页。</li>
</ul>
<p>如果这些预读的数据可以被使用到，那么这样的策略就会提高很大的效率，但是如果这些数据并没有被命中，就会导致把旧的缓存数据给清除掉，反而会降低缓存命中率。</p>
</li>
<li><p>全表扫描</p>
<p>全表扫描会将整张表的页全部读到数据页中，这个时候如果直接将数据替换掉，那么有可能把热点数据全部更新掉了，同样会大大降低查询效率。</p>
</li>
</ul>
<h3 id="划分区域的LRU链表"><a href="#划分区域的LRU链表" class="headerlink" title="划分区域的LRU链表"></a>划分区域的LRU链表</h3><p>为了避免以上两种情况带来的缓存命中率下降，innoDB中的LRU链表设计成了分区域的，一部分热点数据叫young区域，另一部分冷数据叫old区域。</p>
<p><img src="/blog/mysql/bufferPool/lru" alt="lru"></p>
<p>当数据第一次被读到内存中，会先放到old区域，不会直接替换掉young区域中的数据，当数据第二次被读取到的时候会放到young区域，为了避免全表扫描多次读取页的情况，innodb维护了一个系统变量<strong>innodb_old_blocks_time</strong>，当前后两次读取时间小于这个值的时候，并不会直接将old区域的数据直接挪到young区域。</p>
<h2 id="刷新脏页的策略"><a href="#刷新脏页的策略" class="headerlink" title="刷新脏页的策略"></a>刷新脏页的策略</h2><ul>
<li>从LRU冷数据区刷新一部分数据到磁盘</li>
<li>从flush链表刷新页面到磁盘</li>
</ul>
<hr>
<p><img src="/blog/mysql/bufferPool/b.png" alt="bufferpool"></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/mysql/" rel="tag"># mysql</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/mysql/queryCost/" rel="next" title="查询成本计算">
                <i class="fa fa-chevron-left"></i> 查询成本计算
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/mysql/undo/" rel="prev" title="undoLog">
                undoLog <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/uploads/avatar.jpg" alt="arry">
            
              <p class="site-author-name" itemprop="name">arry</p>
              <p class="site-description motion-element" itemprop="description">Java源码/设计模式/个人记录</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">42</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#BufferPool"><span class="nav-number">1.</span> <span class="nav-text">BufferPool</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结构"><span class="nav-number">2.</span> <span class="nav-text">结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#free链表-空闲空间"><span class="nav-number">3.</span> <span class="nav-text">free链表 空闲空间</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#flush链表"><span class="nav-number">4.</span> <span class="nav-text">flush链表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#缓存管理"><span class="nav-number">5.</span> <span class="nav-text">缓存管理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#LRU链表"><span class="nav-number">5.1.</span> <span class="nav-text">LRU链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#划分区域的LRU链表"><span class="nav-number">5.2.</span> <span class="nav-text">划分区域的LRU链表</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#刷新脏页的策略"><span class="nav-number">6.</span> <span class="nav-text">刷新脏页的策略</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">arry</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
